package com.newproject.module;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.IllegalViewOperationException;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by zp on 2019/11/06.
 */
public class ToastModuleB extends ReactContextBaseJavaModule {

    public ToastModuleB(@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @NonNull
    @Override
    public String getName() {
        return "ToastModuleB";
    }

    @ReactMethod
    public void sout(String msg) {
        System.out.println(msg);
    }

    @ReactMethod
    public void setString(String json) {
        System.out.println(json);

        try {
            new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @ReactMethod
    public void measureLayout(String tag, Callback errorCallback, Callback successCallback) {
        try {
            successCallback.invoke(tag + ", 收到啦", "arg1");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void setArray(ReadableArray array) {
        System.out.println("setInt array=" + array);
    }

    @ReactMethod
    public void reject(Promise promise) {
        promise.reject("1", "我就喜欢抛错");
    }

    @ReactMethod
    public void setMap(ReadableMap readableMap, Promise promise) {
        System.out.println("java setMap");
        WritableMap map = Arguments.createMap();
        map.merge(readableMap);
        promise.resolve(map);
    }
}
